package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.Label;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
//因为查询频道就是查询集合列表,所以直接调用list拿到集合
        List<WmChannel> list = list();
        return ResponseResult.okResult(list);

    }

    /**
     * 添加频道
     * @param wmChannel
     */
    @Override
    public ResponseResult add(WmChannel wmChannel) {
       save(wmChannel);
        return ResponseResult.okResult(wmChannel.getId());
    }

    /**
     * 分页查询
     * @param channelDto
     * @return
     */
    @Override
    public ResponseResult PageByquerywapper(ChannelDto channelDto) {
        if (channelDto.getSize()==null||channelDto.getPage()==null){
            channelDto.setPage(1);
            channelDto.setSize(10);
        }
        Page<WmChannel> channelPage = new Page<>(channelDto.getPage(),channelDto.getSize());
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WmChannel::getCreatedTime);
        if (StringUtils.isNotBlank(channelDto.getName())){
            queryWrapper.like(WmChannel::getName,channelDto.getName());
        }
        page(channelPage,queryWrapper);
        ResponseResult result = new PageResponseResult(channelDto.getPage(),channelDto.getSize(), (int) channelPage.getTotal());
        result.setData(channelPage.getRecords());
        return result;
    }

    /**
     * 删除频道大表
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        //先判断状态
        WmChannel wmChannel = this.getById(id);
        if (null!=wmChannel&&null==wmChannel.getStatus()){
          return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
         if (wmChannel.getStatus()){
             return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ACTIVE);
         }
        this.removeById(id);
       return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
@Autowired
private WmNewsService wmNewsService;
    /**
     * 修改
     * @param channel
     * @return
     */
    @Override
    public ResponseResult updatechannel(WmChannel channel) {
        if (null!=channel.getId()){
            WmNews wmNews = wmNewsService.getOne(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, channel.getId()));
            if (wmNews==null){
                updateById(channel);
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        }
       return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);


    }
}
